package com.ztf.reflection.study;

import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;

public class ReflectTest {
    public static void main(String[] args) throws Exception {
//        Class<?> classType = Class.forName("java.lang.String");
//        Method[] mehthods =  classType.getDeclaredMethods();
//        for(Method method :mehthods){
//            System.out.println(method);
//        }
        Class<?> calssType = Customer.class;
        Constructor cons = calssType.getConstructor(new Class[]{String.class,String.class});
        Object customer1 = cons.newInstance(new Object[]{"1231231","123123132"});
        Object customer = calssType.newInstance();

        Field[] fields = calssType.getDeclaredFields();
      //  calssType.getField()
        for(Field field:fields){
            String name = field.getName();
            String firstLetter = name.substring(0,1).toUpperCase();
            String getMethod = "get"+firstLetter+name.substring(1);
            String setMethod = "set"+firstLetter+name.substring(1);
            System.out.println(getMethod+"===="+setMethod);
            Method getmethod1 = calssType.getMethod(getMethod);
            Method setmethod1 = calssType.getMethod(setMethod,new Class[]{String.class});
            String para1 = getmethod1.invoke(customer1)+"";
            System.out.println(name+"="+para1);
            setmethod1.invoke(customer1,new Object[]{"大"});
            String para2 = getmethod1.invoke(customer1)+"";
            System.out.println(name+"="+para2);
        }


    }

}

class Customer{
    private String name;

    private String age;

    public Customer(){

    }
    public  Customer(String name,String age){
        this.age = age;
        this.name = name;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getAge() {
        return age;
    }

    public void setAge(String age) {
        this.age = age;
    }
}